package com.zzyl.serve.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /*@Autowired
    private RedisTemplate redisTemplate;*/

    private static final String CACHE_KEY = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        //根据护理计划的id查询对应的护理项目列表
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        /*List<NursingPlan> o = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (o != null) {
            return  o;
        }*/
        List<NursingPlan> list = nursingPlanMapper.selectNursingPlanList(nursingPlan);
//        redisTemplate.opsForValue().set(CACHE_KEY, list);
        return list;
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        boolean save = save(nursingPlan);
//        redisTemplate.delete(CACHE_KEY);
        return save? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        //bean copy
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        boolean result = updateById(nursingPlan);

        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        //1.删除旧的关联关系
        nursingProjectPlanMapper.delete(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, dto.getId()));

        //2. 添加新的关联关系
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(projectPlan);
        });

//        redisTemplate.delete(CACHE_KEY);
        return result? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean result = removeByIds(Arrays.asList(ids));
//        redisTemplate.delete(CACHE_KEY);
        return  result? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean result = removeById(id);
//        redisTemplate.delete(CACHE_KEY);
        return result? 1 : 0;
    }

    /**
     * 新增护理计划 增强版
     * @param dto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        int flag = insertNursingPlan(nursingPlan);
        //添加护理计划关联的护理项目
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(projectPlan);
        });
        return flag;
    }
}
